<template>
  <div class="app-container">
    <el-table v-loading="listLoading" :data="list" border fit highlight-current-row style="width: 100%">
      <el-table-column align="center" label="编号" width="80">
        <template slot-scope="{row}">
          <span>{{ row.id }}</span>
        </template>
      </el-table-column>
      <el-table-column width="120px" align="center" label="电影">
        <template slot-scope="{row}">
          <span>{{ row.tag }}</span>
        </template>
      </el-table-column>

      <el-table-column width="180px" align="center" label="上映时间">
        <template slot-scope="{row}">
          <span>{{ row.createTime | formatDate }}</span>
        </template>
      </el-table-column>

      <el-table-column min-width="200px" label="优先级">
        <template slot-scope="{row}">
          <template v-if="row.edit">
            <el-input v-model="row.ordinal" class="edit-input" size="small" />
            <el-button
              class="cancel-btn"
              size="small"
              icon="el-icon-refresh"
              type="warning"
              @click="cancelEdit(row)"
            >
              取消
            </el-button>
          </template>
          <span v-else>{{ row.ordinal }}</span>
        </template>
      </el-table-column>

      <el-table-column align="center" label="操作" width="200">
        <template slot-scope="{row}">
          <el-button
            v-if="row.edit"
            type="success"
            size="small"
            icon="el-icon-circle-check-outline"
            @click="confirmEdit(row)"
          >
            确定
          </el-button>
          <el-button
            v-else
            type="primary"
            size="small"
            icon="el-icon-edit"
            @click="row.edit=!row.edit"
          >
            编辑
          </el-button>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<script>
// import { fetchList } from '@/api/article'
import { fetchList, updateLunbo } from '@/api/film'

export default {
  name: 'InlineEditTable',
  filters: {
    formatDate(str){
      return str.substring(0, 10)
    }
  },
  data() {
    return {
      list: null,
      listLoading: true,
      listQuery: {
        page: 1,
        limit: 10
      },
      edited: false // 是否被编辑过
    }
  },
  created() {
    this.getList()
  },
  methods: {
    async getList() {
      this.listLoading = true
      const { data } = await fetchList(this.listQuery)
      const items = data
      this.list = items.map(v => {
        this.$set(v, 'edit', false) // https://vuejs.org/v2/guide/reactivity.html
        v.originalOrdinal = v.ordinal //  will be used when user click the cancel botton
        return v
      })
      this.listLoading = false
    },
    cancelEdit(row) {
      row.ordinal = row.originalOrdinal
      row.edit = false
      this.$message({
        message: '优先级已被恢复为原始值',
        type: 'warning'
      })
      this.refresh();
    },
    async confirmEdit(row) {
      row.edit = false
      row.originalOrdinal = row.ordinal

      let res = await updateLunbo(row)
      this.$message({
        message: '优先级已修改',
        type: 'success'
      })
      this.edited = true;
      this.refresh();
    },
    refresh() {
      if(this.edited) { // 已修改过
        let editing = this.list.some(item => item.edit)
        if (!editing) { // 是否处于编辑状态
          this.getList()
          this.edited = false;
        }
      }
    }
  }
}
</script>

<style scoped>
.edit-input {
  padding-right: 100px;
}
.cancel-btn {
  position: absolute;
  right: 15px;
  top: 10px;
}
</style>
